package
{
	import flash.utils.ByteArray;
	
	import utils.NumberTool;

	public class ModBusCommand_RC608
	{
		private static var single:String="05";
		private static var mutile:String="0F";
		private static var open:String="FF00";
		private static var close:String="00FF";
		public function ModBusCommand_RC608()
		{
		}
		private static function makeDeviceID(id:uint):String
		{
			//输出设备ID
			var str:String=id.toString(16);
			str=NumberTool.fullZeroWidthPosition(str,2,"l");
			return str;
		}
		
		private static function makeAddress(id:uint):String
		{
			//输出继电器地址
			var str:String=id.toString(16);
			str=NumberTool.fullZeroWidthPosition(str,4,"l");
			return str;
		}
		public static function controlMutileToString(diviceId:uint,startId:uint,length:uint,operate:String):String
		{
			var str:String=makeDeviceID(diviceId)+mutile+makeAddress(startId+1)+NumberTool.fullZeroWidthPosition(length.toString(16),4,"l")+"01"+NumberTool.fullZeroWidthPosition(NumberTool.twoTo16(operate),2,"l");
			var endStr:String=makeEnd(str);
			trace("对设备"+diviceId+" 从设备ID为"+(startId+1)+"开始,连续"+length+"个继电器执行"+operate+"指令");
			trace("指令转换为"+NumberTool.fullZeroWidthPosition(NumberTool.twoTo16(operate),2,"l"));
			return str+endStr;
		}
		
		public static function makeEnd(str:String):String
		{
			
			var byte:ByteArray=CRC16JS.toArray(str);
			var endStr:String= CRC16JS.CalculateCrc16(byte);
			endStr=NumberTool.fullZeroWidthPosition(endStr,4,"l")
			return endStr;
		}
		
		
	}
}